package ldk.easytouch.example.menus;

import android.content.Context;
import android.view.View;

import ldk.easytouch.activity.BaseActivity;
import ldk.easytouch.example.Entity.MenuEntity;
import ldk.easytouch.helper.InstrumentHelper;
import ldk.easytouch.utils.ETLog;

/**
 *
 * Created by ke on 16-9-19.
 */
public class ActivityMenuItem extends MikepenzMenuItem implements View.OnClickListener {
    @SuppressWarnings("unused")
    private static final String TAG = "ActivityMenuItem";

    private String mActivityClassName;
    private String mSettingActivityClassName;
    private String mSettingActivityActionName;

    private String mDescStr;
    /*
    * for ActivityMenuItem:
    * data1 -- activity class name, which is extends BaseActivity
    * data2 -- setting activity class name, which is extends Activity
    * data3 -- setting activity class action if it has
    * */
    public static ActivityMenuItem newInstance(Context context, MenuEntity entity){
        ActivityMenuItem result = new ActivityMenuItem(context, entity.getNameStr(), entity.getIconStr());
        result.mActivityClassName = entity.getData1();
        result.mSettingActivityClassName = entity.getData2();
        result.mSettingActivityActionName = entity.getData3();
        result.mDescStr = entity.getDescStr();
        result.setCanDelete(entity.getCanDelete());
        return result;
    }

    public MenuEntity toMenuEntity(){
        MenuEntity result = new MenuEntity();
        result.setData1(mActivityClassName);
        result.setData2(mSettingActivityClassName);
        result.setData3(mSettingActivityActionName);
        result.setId(-1);
        result.setNameStr(name());
        result.setIconStr(getIconStr());
        result.setDescStr(mDescStr);
        result.setCanDelete(canDelete());
        return result;
    }

    public ActivityMenuItem(Context context, String name, String iconStr) {
        super(context, name, iconStr);
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        try {
            Class<BaseActivity> activityClass = (Class<BaseActivity>) Class.forName(mActivityClassName);
            ((BaseActivity)InstrumentHelper.getInstance().getCurrentActivity()).startActivity(activityClass.newInstance());
        } catch (ClassNotFoundException e) {
            ETLog.e(TAG, e.getMessage(), e);
        } catch (InstantiationException e) {
            ETLog.e(TAG, e.getMessage(), e);
        } catch (IllegalAccessException e) {
            ETLog.e(TAG, e.getMessage(), e);
        }
    }
}
